package com.qm.console.controller.rest;

import com.google.common.collect.ImmutableMap;
import com.qm.console.controller.dto.BaseDO;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.controller.dto.ResultEnum;
import com.qm.console.dao.model.SupplementDataDO;
import com.qm.console.dao.model.ValueDisplayConfig;
import com.qm.console.pubdef.dto.anchor.ImMsgQueryDTO;
import com.qm.console.pubdef.dto.anchor.UserActionDTO;
import com.qm.console.pubdef.dto.user.*;
import com.qm.console.service.user.IllegalService;
import com.qm.console.service.user.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(value = "[ 控制台:用户管理 ]", description = "用户相关API")
public class UserController {
    @Autowired
    private UserService UserService;

    @RequiresPermissions("user:view")
    @ApiOperation(value = "用户列表", notes = "查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = false,
                    dataType = "string", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = false,
                    dataType = "string", paramType = "query",defaultValue = "10"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "nickname", value = "用户昵称", required = false,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "ABTest", value = "ABTest", required = false,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "gender", value = "用户性别 1.男，2.女，0.未知", required = false,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "ipAddr", value = "IP地址", required = false,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "channelId", value = "渠道ID", required = false,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "用户手机号", required = false,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "identifyType", value = "认证类型", required = false,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "masterId", value = "用户师傅ID", required = false,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "创建起始时间", required = false,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "创建结束时间", required = false,
                    dataType = "string", paramType = "query")
    })
    @GetMapping("/admin/list")
    public BaseRsp listUserAdmin(@RequestParam(value = "pageNum", required = false, defaultValue = "1") String page,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") String limit,
                                 @RequestParam(value = "userId", required = false) String userId,
                                 @RequestParam(value = "gender", required = false) String gender,
                                 @RequestParam(value = "ipAddr", required = false) String ipAddr,
                                 @RequestParam(value = "nickname", required = false) String nickname,
                                 @RequestParam(value = "ABTest", required = false) String ABTest,
                                 @RequestParam(value = "channelId", required = false) String channelId,
                                 @RequestParam(value = "phone", required = false) String phone,
                                 @RequestParam(value = "identifyType", required = false) String identifyType,
                                 @RequestParam(value = "masterId", required = false) String masterId,
                                 @RequestParam(value = "startTime", required = false) String startTime,
                                 @RequestParam(value = "endTime", required = false) String endTime){
        Map<String, String> paramMap = getGeneralParamMap(limit, page, userId,gender,ipAddr,nickname,channelId,phone,masterId,startTime,endTime);
        if(ABTest != null && ABTest != ""){
            paramMap.put("ABTest", ABTest);
        }
        if(identifyType != null && identifyType != ""){
            paramMap.put("identifyType", identifyType);
        }
        return UserService.listUserAdmin(paramMap);
    }

    @GetMapping("/channel")
    public BaseRsp listChannelAdmin(@RequestParam(value = "limit", required = false, defaultValue = "20") String limit,
                                 @RequestParam(value = "page", required = false, defaultValue = "1") String page){
        Map paramMap=new HashMap<>();
        paramMap.put("pageSize",limit);
        paramMap.put("pageNum",page);
        return UserService.listChannelAdmin(paramMap);
    }

    @GetMapping("/detail")
    public BaseRsp userDetailAdmin(@RequestParam(value = "userId", required = true) String userId) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("userId",userId);
        return UserService.userDetailAdmin(paramMap);
    }
//    @GetMapping("/detail")
//    public BaseRsp userDetailAdmin(@RequestParam(value = "userId", required = true) String userId){
//        Map<String, String> paramMap=new HashMap<>();
//        paramMap.put("userId",userId);
//        return UserService.userDetailAdmin(paramMap);
//    }

//    @PostMapping("/forbid")
//    public BaseRsp userForbidAdmin(@RequestBody UserForbidAdminDTO updateDTO){
//        return UserService.updateUserForbidAdmin(updateDTO);
//    }

    private Map<String, String> getGeneralParamMap(String limit,
                                                   String page,
                                                   String userId,
                                                   String gender,
                                                   String ipAddr,
                                                   String nickname,
                                                   String channelId,
                                                   String phone,
                                                   String masterId,
                                                   String startTime,
                                                   String endTime){
        Map paramMap= new HashMap<>();
        paramMap.put("pageSize", limit);
        paramMap.put("pageNum", page);
        if(userId != null && userId != ""){
            paramMap.put("userId", userId);
        }
        if(gender != null){
            paramMap.put("gender", gender);
        }
        if(ipAddr != null && ipAddr != ""){
            paramMap.put("ipAddr", ipAddr);
        }
        if(nickname != null && nickname != ""){
            paramMap.put("nickname", nickname);
        }
        if(channelId != null && channelId != ""){
            paramMap.put("channelId", channelId);
        }
        if(phone != null){
            paramMap.put("phone", phone);
        }
        if(masterId != null){
            paramMap.put("masterId", masterId);
        }
        if(startTime != null){
            paramMap.put("startTime", startTime);
//            String startTimestamp = String.valueOf((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(startTime, new ParsePosition(0)).getTime());
//            paramMap.put("startTime", startTimestamp);
        }
        if(endTime != null){
            paramMap.put("endTime", endTime);
//            String endTimestamp = String.valueOf((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(endTime, new ParsePosition(0)).getTime());
//            paramMap.put("endTime", endTimestamp);
        }
        return paramMap;
    }

    @GetMapping("prop/list")
    public BaseRsp listPropChange(PropChangeQueryDTO query) {
        return UserService.listPropChanges(query);
    }

    @GetMapping("prop/change")
    public BaseRsp propChangeCount(PropChangeQueryDTO query) {
        return UserService.propChangeCount(query);
    }

    @GetMapping("prop/options")
    public BaseRsp listPropChangeChannels() {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of(
                "channels", UserService.listPropChannels(),
                "types", UserService.listPropTypes(),
                "reasons", UserService.listPropReasons()
        ));
    }

    @PostMapping("prop/change/value")
    public BaseRsp PropChangeValue(@RequestBody ValueDisplayConfig query) {
        UserService.PropChangeValue(query);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("message", "ok"));
    }

    /**
     * 【补单列表接口】
     * @return
     */
    @ApiOperation(value = "运营补单", notes = "补单列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = false, dataType = "int", paramType = "query",defaultValue = "10"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "nickname", value = "用户昵称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "创建起始时间", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "创建结束时间", required = false, dataType = "string", paramType = "query")
    })
    @GetMapping("/supplement")
    public BaseDO<SupplementDataDO> supplementList(@RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "limit", required = false, defaultValue = "20") Integer pageSize,
                                                   @RequestParam(value = "userId", required = false) Long userId,
                                                   @RequestParam(value = "nickname", required = false) String nickname,
                                                   @RequestParam(value = "startTime", required = false) String startTime,
                                                   @RequestParam(value = "endTime", required = false) String endTime
                                  ){
        Map paramMap=new HashMap<>();
        paramMap.put("pageSize",pageSize);
        paramMap.put("pageNum",pageNum);
        paramMap.put("userId",userId);
        paramMap.put("nickname",nickname);
//        paramMap.put("startTimestamp", startTime==null?null:(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(startTime, new ParsePosition(0)).getTime());
        paramMap.put("startTimestamp", startTime);
//        paramMap.put("endTimestamp", endTime==null?null:(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(endTime, new ParsePosition(0)).getTime());
        paramMap.put("endTimestamp", endTime);
        return UserService.supplementList(paramMap);
    }

    /**
     * 【新建补单接口】
     * @param params
     * @return
     */
    @ApiOperation(value = "新建运营补单", notes = "新建补单接口")
    @PostMapping("/supplement/add")
    public BaseRsp supplementAdd(@RequestBody SupplementDTO params
                                 ){
        System.out.println(params);
        return UserService.supplementAdd(params);
    }

    /**
     * 【操作补单接口】
     * @param params
     * @return
     */
    @ApiOperation(value = "操作运营补单", notes = "操作补单接口")
    @PostMapping("/supplement/dispose")
    public BaseRsp supplementDispose(@RequestBody SupplementDisposeDTO params
    ){
        return UserService.supplementDispose(params);
    }

    /**
     * 【主播详情接口】
     * @return
     */
    @ApiOperation(value = "主播详情页面", notes = "主播详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "主播ID", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "创建起始时间", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "创建结束时间", required = false, dataType = "string", paramType = "query")
    })
    @GetMapping("/anchor/detail")
    public BaseDO<AnchorDetailDTO> anchorList(@RequestParam(value = "userId", required = false) String userId,
                              @RequestParam(value = "startTime", required = false) String startTime,
                              @RequestParam(value = "endTime", required = false) String endTime
    ){
        Map paramMap=new HashMap<>();
        paramMap.put("userId",userId);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        return UserService.anchorDetail(paramMap);
    }

    /**
     * 【用户举报接口】
     * @return
     */
    @ApiOperation(value = "用户举报页面", notes = "用户举报接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, dataType = "string", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = false, dataType = "string", paramType = "query",defaultValue = "10"),
            @ApiImplicitParam(name = "userId", value = "被举报ID/举报人ID", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "gender", value = "被举报性别", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "entranceType", value = "举报场景", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "reportType", value = "举报原因 0、广告骚扰 1、淫秽色情 2、攻击谩骂 3、虚假信息 4、诈骗 5、政治敏感, -1为用户自定义\"其他", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "reportStatus", value = "举报状态 0、正在处理中 1、处理完成，暂未违规 2、处理完成，管理后台填写了反馈建议", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "string", paramType = "query")
    })
    @GetMapping("/accuse/list")
    public BaseDO<AccuseInfoDTO> accuseList(@RequestParam(value = "page", required = false, defaultValue = "1") String pageNum,
                              @RequestParam(value = "limit", required = false, defaultValue = "20") String pageSize,
                              @RequestParam(value = "userId", required = false) String userId,
                              @RequestParam(value = "gender", required = false) String gender,
                              @RequestParam(value = "entranceType", required = false) String entranceType,
                              @RequestParam(value = "reportType", required = false) String reportType,
                              @RequestParam(value = "reportStatus", required = false) String reportStatus,
                              @RequestParam(value = "startTime", required = false) String startTime,
                              @RequestParam(value = "endTime", required = false) String endTime
    ){
        Map<String, String> paramMap=new HashMap<>();
        paramMap.put("pageSize",pageSize);
        paramMap.put("pageNum",pageNum);
        paramMap.put("userId",userId);
//        paramMap.put("gender",gender);
        paramMap.put("targetGender",gender);
        paramMap.put("entranceType",entranceType);
        paramMap.put("reportType",reportType);
        paramMap.put("reportStatus",reportStatus);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);

        return UserService.accuseList(paramMap);
    }

    /**
     * 【用户举报处理接口】
     *
     */
    @ApiOperation(value = "用户举报处理", notes = "用户举报处理接口")
    @PostMapping("/accuse/dispose")
    public BaseRsp accuseDispose(@RequestBody AccuseDisposeDTO params
    ){
        return UserService.accuseDispose(params);
    }


    /**
     * 【用户信息 - 充值订单页面】
     * @return
     */
    @ApiOperation(value = "充值订单", notes = "充值订单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, dataType = "string", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = false, dataType = "string", paramType = "query",defaultValue = "10"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "nickname", value = "用户昵称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "registerChannel", value = "注册渠道", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "payChannel", value = "支付渠道", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "rechargeId", value = "计费点id", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "rechargeName", value = "计费点名称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "rechargeType", value = "计费点类型", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "countryCode", value = "国家代码", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "payStatus", value = "支付状态 0.未支付，1.已支付", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "string", paramType = "query")
    })
    @GetMapping("/order/list")
    public BaseRsp orderList(@RequestParam(value = "page", required = false, defaultValue = "1") String pageNum,
                                            @RequestParam(value = "limit", required = false, defaultValue = "20") String pageSize,
                                            @RequestParam(value = "userId", required = false) String userId,
                                            @RequestParam(value = "nickname", required = false) String nickname,
                                            @RequestParam(value = "registerChannel", required = false) String registerChannel,
                                            @RequestParam(value = "payChannel", required = false) String payChannel,
                                            @RequestParam(value = "countryCode", required = false) String countryCode,
                                            @RequestParam(value = "rechargeId", required = false) String rechargeId,
                                            @RequestParam(value = "rechargeName", required = false) String rechargeName,
                                            @RequestParam(value = "rechargeType", required = false) String rechargeType,
                                            @RequestParam(value = "payStatus", required = false) String payStatus,
                                            @RequestParam(value = "startTime", required = false) String startTime,
                                            @RequestParam(value = "endTime", required = false) String endTime
    ){
        Map<String, String> paramMap=new HashMap<>();
        paramMap.put("pageSize",pageSize);
        paramMap.put("pageNum",pageNum);
        paramMap.put("userId",userId);
        paramMap.put("nickname",nickname);
        paramMap.put("registerChannel",registerChannel);
        paramMap.put("payChannel",payChannel);
        paramMap.put("rechargeId",rechargeId);
        paramMap.put("rechargeName",rechargeName);
        paramMap.put("rechargeType",rechargeType);
        paramMap.put("payStatus",payStatus);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        paramMap.put("countryCode", countryCode);

        return UserService.orderList(paramMap);
    }
    /**
     * 【用户信息 - 充值订单页面】
     * @return
     */
    @ApiOperation(value = "充值订单计费点查询", notes = "充值订单计费点查询")
    @GetMapping("/charge_unit/list")
    public BaseRsp chargeUnitList(){
        return UserService.chargeUnitList();
    }

    @GetMapping("/get_card_ticket")
    public BaseRsp userDetail(@RequestParam(value = "userId", required = true) String userId){
        PropChangeQueryDTO query = new PropChangeQueryDTO();
        query.setUserId(Integer.parseInt(userId));
        return UserService.userProp(query);
    }

    @GetMapping("/get_diamond")
    public BaseRsp userRecharge(@RequestParam(value = "userId", required = true) String userId){
        PropChangeQueryDTO query = new PropChangeQueryDTO();
        query.setUserId(Integer.parseInt(userId));
        return UserService.userRecharge(query);
    }

    @GetMapping("/get_score")
    public BaseRsp userWithdraw(@RequestParam(value = "userId", required = true) String userId){
        PropChangeQueryDTO query = new PropChangeQueryDTO();
        query.setUserId(Integer.parseInt(userId));
        return UserService.userWithdraw(query);
    }

    @GetMapping("/action/list")
    public BaseRsp userAction(UserActionDTO paramMap){
//        Map<String, String> paramMap = new HashMap<>();
//        paramMap.put("accountId", accountId);
//        paramMap.put("action", action);
//        paramMap.put("pageSize",pageSize);
//        paramMap.put("pageNum",pageNum);
//        paramMap.put("startTime", startTime);
//        paramMap.put("endTime", endTime);
        return UserService.userAction(paramMap);
    }


    @GetMapping("/get_chat_gift")
    public BaseRsp userChatGift(@RequestParam(value = "userId", required = true) String userId){
        ImMsgQueryDTO params = new ImMsgQueryDTO();
        params.setUserId(Integer.parseInt(userId));
        return UserService.userChatGift(params);
    }

    @GetMapping("/get_chat_gift/list")
    public BaseRsp userChatGiftList(VideoVoiceListDTO query){
        return UserService.userChatGiftList(query);
    }

    @ApiOperation(value = "用户详情 取消真人认证", notes = "取消真人认证接口")
    @PostMapping("/detail/cancelLive")
    public BaseRsp cancelLive(@RequestBody userInfoDTO userInfo
    ){
        String userId = userInfo.getUserId();
        return UserService.cancelLive(userId);
    }

    @ApiOperation(value = "用户详情 女神认证", notes = "女神认证接口")
    @PostMapping("/detail/confirmAnchor")
    public BaseRsp confirmAnchor(@RequestBody userInfoDTO userInfo
    ){
        String userId = userInfo.getUserId();
        return UserService.confirmAnchor(userId);
    }

    // 消息推送相关接口.

//    违规记录相关接口
    @Autowired
    private IllegalService illegalRecordService;

    @GetMapping("illegal")
    @ApiOperation("违规记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "offset", value = "当前页码", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = false, dataType = "int", paramType = "query",defaultValue = "10"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "gender", value = "性别", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "read", value = "已读", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "sceneType", value = "场景 0-头像, 1-形象墙 2-封面, 3-真人认证, 4-动态 6-im图片 7-im文字", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "string", paramType = "query")
    })
//
//       '图像审核': 0,
//               '形象墙审核': 1,
//               '封面审核': 2,
//               '真人认证': 3,
//               '动态图片审核': 4,
//               '动态视频审核': 5,
//               'im图片': 6,
//               'im文字': 7
    public BaseRsp list(IllegalRecordQueryDTO queryDTO) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", illegalRecordService.list(queryDTO), "count", illegalRecordService.count(queryDTO)));
    }

    @PostMapping("illegal")
    @ApiOperation("设置已读")
    public BaseRsp check(@RequestBody List<Integer> ids) {
        illegalRecordService.setRead(ids);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/illegal/keyword")
    public BaseRsp keywords() {
        return new BaseRsp(ResultEnum.SUCCESS, illegalRecordService.listKeywords());
    }

    @PostMapping("/illegal/keyword")
    public BaseRsp addKeyword(@RequestBody Map<String, String> keyword) {
        illegalRecordService.addKeyword(keyword.get("keyword"));
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/illegal/keyword/{id}")
    public BaseRsp removeKeyword(@PathVariable("id") Integer id) {
        illegalRecordService.deleteKeyword(id);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

}
